package org.ydeity.lucene.configuration;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.lucene.document.Field;
import org.ydeity.lucene.annotation.FieldType;
import org.ydeity.lucene.annotation.LuceneField;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class LuceneFieldProperties {

    private FieldType fieldType;

    private boolean store;

    private String searchAnalyzer;

    private String analyzer;

    private Class<?> paramType;

    public LuceneFieldProperties(LuceneField luceneField, Class<?> paramType) {
        this.fieldType = luceneField.type();
        this.store = luceneField.store();
        this.searchAnalyzer = luceneField.searchAnalyzer();
        this.analyzer = luceneField.analyzer();
        this.paramType = paramType;
    }

    public Field.Store isStore(){
        if(this.store) {
            return Field.Store.YES;
        }
        return Field.Store.NO;
    }

    public FieldType getFieldType(){
        if(this.fieldType != null && !FieldType.Auto.equals(this.fieldType)){
            return this.fieldType;
        }

        if(this.paramType.equals(String.class)){
            return FieldType.Auto;
        }

        if(this.paramType.equals(Integer.class) || this.paramType.equals(int.class)){
            return FieldType.Integer;
        }

        if(this.paramType.equals(Long.class) || this.paramType.equals(long.class)){
            return FieldType.Long;
        }

        if(this.paramType.equals(Short.class) || this.paramType.equals(short.class)){
            return FieldType.Integer;
        }

        if(this.paramType.equals(Double.class) || this.paramType.equals(double.class)){
            return FieldType.Double;
        }

        if(this.paramType.equals(Boolean.class) || this.paramType.equals(boolean.class)){
            return FieldType.Integer;
        }

        if(this.paramType.equals(BigDecimal.class)){
            return FieldType.Double;
        }

        if(this.paramType.equals(Date.class) || this.paramType.equals(LocalDateTime.class) || this.paramType.equals(LocalDate.class)){
            return FieldType.Date;
        }

        return FieldType.Auto;
    }
}
